package com.ciwong.epaper.modules.me.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.modules.me.bean.ServiceDetail;
import com.ciwong.epaper.modules.scan.adapter.BaseAdapter;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 品牌adapter Created by res-zwyan on 2015/9/23.
 */
public class ServicesAdapter extends BaseAdapter<Service>
{
    private List<ServiceDetail> serviceDetails;

    private OnItemClickListener listener;

    protected ServicesAdapter(List list)
    {
        super(list);
    }

    public ServicesAdapter(Activity act, List list,
            OnItemClickListener listener)
    {
        super(act, list);
        this.listener = listener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view;
        if (convertView == null)
        {
            view = View.inflate(act, R.layout.item_service, null);
        }
        else
        {
            view = convertView;
        }
       /* ImageView brandIcon = ViewHolder.get(view, R.id.iv_brand_item_icon);
        TextView brandName = ViewHolder.get(view, R.id.tv_item_brandname);*/

        ImageView iv_brand_item_icon= ViewHolder.get(view,R.id.iv_brand_item_icon);
        TextView tv_item_brandname=ViewHolder.get(view,R.id.tv_item_brandname);
        TextView the_num_buy_tv=ViewHolder.get(view,R.id.the_num_buy_tv);
        TextView price_now=ViewHolder.get(view,R.id.price_now);
        TextView money_singn1=ViewHolder.get(view,R.id.money_singn1);
        TextView tv_discoint=ViewHolder.get(view,R.id.tv_discoint);
        TextView adpter_area_tv_tips=ViewHolder.get(view,R.id.adpter_area_tv_tips);
        TextView area_tv=ViewHolder.get(view,R.id.area_tv);
        TextView perid_tv=ViewHolder.get(view,R.id.perid_tv);



       /* Button serviceExperiment = ViewHolder.get(view,
                R.id.services_experiment);
        ImageView servicesArrow = ViewHolder.get(view, R.id.services_arrow);
*/
        Service serivce = (Service) getItem(position);
        if (listener != null)
        {
            listener.onItemClick(view, serivce);
        }
        String logoUrl = serivce.getLogo();
        ImageLoader.getInstance()
                .displayImage(TextUtils.isEmpty(logoUrl)
                        ? "drawable://" + R.mipmap.brand_loading : logoUrl, iv_brand_item_icon,
                EConstants.getServiceLogoOption(act));
        tv_item_brandname.setText(serivce.getName());
       /* if (serviceDetails == null || serviceDetails.isEmpty())
        {
            serviceExperiment.setVisibility(View.VISIBLE);
            servicesArrow.setVisibility(View.GONE);
        }
        else
        {
            ServiceDetail serviceDetail = new ServiceDetail();
            serviceDetail.setServiceType(serivce.getId());
            serviceDetail.setbExpired(true);
            int indexOf = serviceDetails.indexOf(serviceDetail);
            if (indexOf >= 0)
            {
                serviceDetail = serviceDetails.get(indexOf);
            }
            if (serviceDetail.getOntrialCount() <= 0
                    && serviceDetail.isbExpired())
            {
                serviceExperiment.setVisibility(View.VISIBLE);
                servicesArrow.setVisibility(View.GONE);
            }
            else
            {
                serviceExperiment.setVisibility(View.GONE);
                servicesArrow.setVisibility(View.VISIBLE);
            }
        }*/
        return view;
    }

    /**
     * 清空数据集合
     */
    public void clear()
    {
        if (this.mList != null && !this.mList.isEmpty())
        {
            this.mList.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据集合
     *
     * @param services
     *            服务信息集合
     */
    public void addAll(List<Service> services)
    {
        if (this.mList != null)
        {
            this.mList.addAll(services);
            notifyDataSetChanged();
        }
    }

    /**
     * 设置服务详情列表
     * 
     * @param serviceDetails
     *            服务详情列表
     */
    public void setServiceDetails(List<ServiceDetail> serviceDetails)
    {
        if (serviceDetails != null)
        {
            this.serviceDetails = serviceDetails;
            notifyDataSetChanged();
        }
    }

    public static interface OnItemClickListener
    {
        void onItemClick(View view, Object object);
    }
}
